﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class NewProblemReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ProblemReportService service = new ProblemReportService();
                List<ReportType> reportTypes = new List<ReportType>();
                reportTypes.Add(new ReportType { ID=0, Name="请选择报告类型" });
                reportTypes.AddRange(service.GetReportTypeList().ToList());
                reportTypeDDL.DataSource = reportTypes;
                reportTypeDDL.DataBind();
                reportTypeDDL1.DataSource = reportTypes;
                reportTypeDDL1.DataBind();

                List<ProductLineType> productLineTypes = new List<ProductLineType>();
                productLineTypes.Add(new ProductLineType { ID=0, Name="请选择产品线" });
                productLineTypes.AddRange(service.GetProductLineTypeList().ToList());
                productLineTypeDDL.DataSource = productLineTypes;
                productLineTypeDDL.DataBind();
                productLineTypeDDL1.DataSource = productLineTypes;
                productLineTypeDDL1.DataBind();

                ProblemTypeService problemTypeService = new ProblemTypeService();
                List<ProblemType> problemTypes = new List<ProblemType>();
                problemTypes.Add(new ProblemType { ID = 0, Name = "请选择故障类型" });
                problemTypes.AddRange(problemTypeService.GetAllProblemType());
                problemTypeDDL.DataSource = problemTypes;
                problemTypeDDL.DataBind();

                ProductClassService productClassService = new ProductClassService();
                List<ProductVersion> productVersions = new List<ProductVersion>();
                productVersions.Add(new ProductVersion { ID=0, Name="请选择产品版本" });
                productVersions.AddRange(productClassService.GetVersions().ToList());
                productVersionDDL.DataSource = productVersions;
                productVersionDDL.DataBind();
                productVersionDDL1.DataSource = productVersions;
                productVersionDDL1.DataBind();
            }
        }

        protected void NewProblemBtn_Click(object sender, EventArgs e)
        {
            string errorMsg = string.Empty;
            if (reportTypeDDL.SelectedValue.Equals("0"))
            {
                errorMsg = "请选择报告类型;";
            }
            if (productLineTypeDDL.SelectedValue.Equals("0"))
            {
                errorMsg += "请选择产品线;";
            }
            if (problemTypeDDL.SelectedValue.Equals("0"))
            {
                errorMsg += "请选择故障类型;";
            }
            if (string.IsNullOrEmpty(problemReasonTxtBox.Text.Trim()))
            {
                errorMsg += "请填写故障原因;";
            }
            if (string.IsNullOrEmpty(productModelTxtBox.Text.Trim()))
            {
                errorMsg += "请填写产品型号;";
            }
            if (productVersionDDL.SelectedValue.Equals("0"))
            {
                errorMsg += "请选择产品版本;";
            }
            if (string.IsNullOrEmpty(subjectTxtBox.Text.Trim()))
            {
                errorMsg += "请填写故障报告主题;";
            }
            if (string.IsNullOrEmpty(descriptionTxtBox.Text.Trim()))
            {
                errorMsg += "请填写客户描述/故障现象;";
            }

            if (!string.IsNullOrEmpty(errorMsg))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "tip", string.Format("alert('错误提示:{0}');", errorMsg), true);
            }
            else
            {
                ProblemReport problemReportEntity = new ProblemReport();
                problemReportEntity.ProblemDescription = descriptionTxtBox.Text.Trim();
                problemReportEntity.ProblemReason = problemReasonTxtBox.Text.Trim();
                problemReportEntity.ProblemStatus = "未处理";
                problemReportEntity.ProblemType = problemTypeDDL.SelectedItem.Text;
                problemReportEntity.ProductLineType = productLineTypeDDL.SelectedItem.Text;
                problemReportEntity.ProductType = productModelTxtBox.Text.Trim();
                problemReportEntity.ProductVersion = productVersionDDL.SelectedItem.Text;
                problemReportEntity.ReportType = reportTypeDDL.SelectedItem.Text;
                problemReportEntity.SecurityType = SecurityRadioButtonList.SelectedValue;
                problemReportEntity.Solution = solutionTxtBox.Text.Trim();
                problemReportEntity.Subject = subjectTxtBox.Text.Trim();
                problemReportEntity.Code = "P";
                UserService userService = new UserService();
                problemReportEntity.User = userService.GetUserByID(ContextService.CurrentAccount.UserID);
                problemReportEntity.CreateDate = DateTime.Now;
                ProblemReportService problemReportService = new ProblemReportService();                
                problemReportService.AddProblemReport(problemReportEntity);
                Response.Write(string.Format("<script language='JavaScript'> alert( '故障报告已成功创建！');location.href= 'ProblemReportManage.aspx?T=0&P=0' </script> ")); 
            }
        }

        protected void NewReportBtn_Click(object sender, EventArgs e)
        {
            string errorMsg = string.Empty;
            if (reportTypeDDL1.SelectedValue.Equals("0"))
            {
                errorMsg = "请选择报告类型;";
            }
            if (productLineTypeDDL1.SelectedValue.Equals("0"))
            {
                errorMsg += "请选择产品线;";
            }
            if (string.IsNullOrEmpty(productModelTxtBox1.Text.Trim()))
            {
                errorMsg += "请填择写产品型号;";
            }
            if (productVersionDDL1.SelectedValue.Equals("0"))
            {
                errorMsg += "请选择产品版本;";
            }
            if (string.IsNullOrEmpty(subjectTxtBox1.Text.Trim()))
            {
                errorMsg += "请填写资料报告主题;";
            }

            if (!string.IsNullOrEmpty(errorMsg))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "tip", string.Format("alert('错误提示:{0}');", errorMsg), true);
            }
            else
            {
                ProblemReport problemReportEntity = new ProblemReport();
                problemReportEntity.ProductLineType = productLineTypeDDL1.SelectedItem.Text;
                problemReportEntity.ProductType = productModelTxtBox1.Text.Trim();
                problemReportEntity.ProductVersion = productVersionDDL1.SelectedItem.Text;
                problemReportEntity.ReportType = reportTypeDDL1.SelectedItem.Text;
                problemReportEntity.SecurityType = SecurityRadioButtonList1.SelectedValue;
                problemReportEntity.Solution = solutionTxtBox1.Text.Trim();
                problemReportEntity.Subject = subjectTxtBox1.Text.Trim();
                problemReportEntity.Code = "R";
                UserService userService = new UserService();
                problemReportEntity.User =userService.GetUserByID(ContextService.CurrentAccount.UserID);
                problemReportEntity.CreateDate = DateTime.Now;
                ProblemReportService problemReportService = new ProblemReportService();
                problemReportService.AddProblemReport(problemReportEntity);
                Response.Write(string.Format("<script language='JavaScript'> alert( '资料报告已成功创建！');location.href= 'ProblemReportManage.aspx?T=0&P=0' </script> ")); 
            }
        }
    }
}
